<?php
namespace Unicom\Controller;
use Think\Controller;

class StatController extends AdminController {

	public function index() {
		layout(false);
		$this->assign('date', date('Y-m-d'));
		$this->display();
	}

	// /**
	//  *
	//  * @param  [type] $date [description]
	//  * @param  [type] $type 0 包月 1 点播
	//  * @return [type]       [description]
	//  */
	// public function statMonth(){
	// 	$date = I('param.date',date('Y-m-d'),'string');
	// 	$type = I('param.type',0,'int');
	// 	layout(false);
	// 	$servcode1="('WV','WD','WS')";
	//   	$servcode2="('WX1234','WX2','WX3','WX4','WX5678','WX81','WX82')";
	// 	$model = M('cp.privince','cp_');
	// 	$where = $type?" v.mocontent in {$servcode2}":" v.mocontent in {$servcode1}";
	// 	$sql_month = "select v.id,p.dcprivence,p.dccity,count(*) as count

	// 	  from cp.cp_verify_phone  as v left join cp.cp_privence as p
	// 	  on left (v.mophone,7)=p.dcmobile
	// 	  where DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}'"." and {$where}".
	// 	  " group by p.dcprivence";
	// 	$sql_count="select count(*) total from cp.cp_verify_phone as v where DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}' and {$where} ";
	// 	$total_count = $model->query($sql_count)[0]['total'];
	// 	$data = $model->query($sql_month); //按省份统计
	// 	$ret['total']=$total_count;
	// 	$ret['data']=$data;

	// 	$this->ajaxReturn($ret);
	// 	//$this->display();

	// }

	public function statMonth() {
		$date = I('param.date', date('Y-m-d'), 'string');
		$type = I('param.type', 0, 'int');
		layout(false);
		$servcode1 = "('WV','WD','WS')";
		$servcode2 = "('WX1234','WX2','WX3','WX4','WX5678','WX81','WX82')";
		$table = "cp.cp_order_log";
		$model = M('cp.privince', 'cp_');
		$where = "v.mocontent in {$servcode1}";
		$sql_month = "select v.id,p.dcprivence,p.dccity,count(*) as count



		  from (select per.id,per.create_time,per.phone,ver.mocontent from cp.cp_order_log as per inner join cp.cp_verify_phone as ver on per.linkid=ver.linkid where DATE_FORMAT(per.create_time,'%Y-%m-%d')='{$date}')  as v left join cp.cp_privence as p
		  on left (v.phone,7)=p.dcmobile
		  where DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}'" . " and {$where}" .
			" group by p.dcprivence";
		$sql_count = "select count(*) total from cp.cp_order_log as p inner join cp.cp_verify_phone as v on p.linkid=v.linkid where DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}' and DATE_FORMAT(p.create_time,'%Y-%m-%d')='{$date}' and {$where} ";
		$total_count = $model->query($sql_count)[0]['total'];
		$data = $model->query($sql_month); //按省份统计
		$ret['total'] = $total_count;
		$ret['data'] = $data;

		$this->ajaxReturn($ret);

	}

	public function statDianbo() {
		$date = I('param.date', date('Y-m-d'), 'string');
		$type = I('param.type', 0, 'int');
		layout(false);
		$servcode1 = "('WV','WD','WS')";
		$servcode2 = "('WX1234','WX2','WX3','WX4','WX5678','WX81','WX82')";
		$table = "cp.cp_order_log";
		$model = M('cp.privince', 'cp_');
		$where = "left(v.mocontent,2)='WX'";
		$sql_month = "select v.id,p.dcprivence,p.dccity,count(*) as count



		  from (select p.id,p.create_time,p.phone,v.mocontent from cp.cp_payment_log as p inner join cp.cp_verify_phone as v on p.linkid=v.linkid where DATE_FORMAT(p.create_time,'%Y-%m-%d')='{$date}' and DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}' and p.msgtype=2 and status=0 and $where)  as v left join cp.cp_privence as p
		  on left (v.phone,7)=p.dcmobile group by p.dcprivence";
		$sql_count = "select count(*) total from cp.cp_payment_log as p inner join cp.cp_verify_phone as v on p.linkid=v.linkid where DATE_FORMAT(p.create_time,'%Y-%m-%d')='{$date}' and DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}' and {$where} and p.msgtype=2 and p.status=0 ";
		$total_count = $model->query($sql_count)[0]['total'];
		$data = $model->query($sql_month); //按省份统计
		$ret['total'] = $total_count;
		$ret['data'] = $data;
		//var_dump($ret);
		$this->ajaxReturn($ret);

	}

	public function delete() {
		$model = M('cp.privence', 'cp_');
		$sql = "select * from cp.cp_privence group by dcmobile having count(dcmobile)>1";
		$data = $model->query($sql);
		foreach ($data as $key => $value) {
			$ret = $model->where(array('id' => $value['id']))->delete();
			var_dump("ret=$ret");
		}

		var_dump("处理完成");

	}

	public function count() {
		layout(false);
		if (IS_POST) {
			$date_time = I('param.date_time', '', 'string');
			$date_end = I('param.date_end', '', 'string');
			$type = I('param.type', 0, 'int');

			$table_name = "cp_verify_phone";
			$headers = array();
			switch ($type) {
			case 0:
				$table_name = "cp_verify_phone ";

				break;
			case 1:
				$table_name = "cp_payment_log ";

				break;
			case 2:
				$table_name = "cp_order_log ";

				break;

			default:
				# code...
				break;
			}
			$where = $date_time ? "where status=0 and DATE_FORMAT( create_time, '%Y-%m-%d' ) between '" . $date_time . "'" . "and '" . $date_end . "'" : "";
			$sql = "SELECT count(*) FROM " . $table_name . $where . " order by create_time desc";
			$model = M('cp.verify_phone', 'cp_');
			$data = $model->query($sql);
			var_dump($data);

		} else {
			$this->display();
		}

	}
}